package com.project.movie.controller.comment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.*;
import com.project.movie.service.MovieCommentService;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieOrderService;
import com.project.movie.service.MovieSliceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 电影评论controller
 * @date 2025/02/06 11:05
 */
@Controller
@ResponseBody
@RequestMapping("comment")
public class MovieCommentController {

    @Autowired
    private MovieCommentService movieCommentService;
    @Autowired
    private MovieItemService movieItemService;
    @Autowired
    private MovieOrderService movieOrderService;

    /** 分页获取电影评论 */
    @PostMapping("getMovieCommentPage")
    public Result getMovieCommentPage(@RequestBody MovieComment movieComment) {
        Page<MovieComment> page = new Page<>(movieComment.getPageNumber(),movieComment.getPageSize());
        QueryWrapper<MovieComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieComment.getContent()),MovieComment::getContent,movieComment.getContent())
                .eq(movieComment.getStar() != null,MovieComment::getStar,movieComment.getStar())
                .eq(StringUtils.isNotBlank(movieComment.getItemId()),MovieComment::getItemId,movieComment.getItemId())
                .eq(StringUtils.isNotBlank(movieComment.getCreateBy()),MovieComment::getCreateBy,movieComment.getCreateBy())
                .eq(movieComment.getCreateTime() != null,MovieComment::getCreateTime,movieComment.getCreateTime())
                .eq(StringUtils.isNotBlank(movieComment.getUpdateBy()),MovieComment::getUpdateBy,movieComment.getUpdateBy())
                .eq(movieComment.getUpdateTime() != null,MovieComment::getUpdateTime,movieComment.getUpdateTime());
        Page<MovieComment> movieCommentPage = movieCommentService.page(page, queryWrapper);
        return Result.success(movieCommentPage);
    }

    @PostMapping("getMovieCommentList")
    public Result getMovieCommentList(@RequestBody MovieComment movieComment) {
        QueryWrapper<MovieComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieComment.getContent()),MovieComment::getContent,movieComment.getContent())
                .eq(movieComment.getStar() != null,MovieComment::getStar,movieComment.getStar())
                .eq(StringUtils.isNotBlank(movieComment.getItemId()),MovieComment::getItemId,movieComment.getItemId())
                .eq(StringUtils.isNotBlank(movieComment.getCreateBy()),MovieComment::getCreateBy,movieComment.getCreateBy())
                .eq(movieComment.getCreateTime() != null,MovieComment::getCreateTime,movieComment.getCreateTime())
                .eq(StringUtils.isNotBlank(movieComment.getUpdateBy()),MovieComment::getUpdateBy,movieComment.getUpdateBy())
                .eq(movieComment.getUpdateTime() != null,MovieComment::getUpdateTime,movieComment.getUpdateTime());
        List<MovieComment> movieCommentPage = movieCommentService.list(queryWrapper);
        return Result.success(movieCommentPage);
    }

    /** 根据id获取电影评论 */
    @GetMapping("getMovieCommentById")
    public Result getMovieCommentById(@RequestParam("id")String id) {
        MovieComment movieComment = movieCommentService.getById(id);
        return Result.success(movieComment);
    }

    /** 保存电影评论 */
    @PostMapping("saveMovieComment")
    public Result saveMovieComment(@RequestBody MovieComment movieComment) throws ParseException {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<MovieComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieComment::getOrderId,movieComment.getOrderId())
                .eq(MovieComment::getUserId,userInfo.getId());
        int count = movieCommentService.count(queryWrapper);
        if (count > 0) {
            return Result.fail("已评论过影片无法再次评论");
        }
        //判断评论时间
        MovieOrder movieOrder = movieOrderService.getById(movieComment.getOrderId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String string = format.format(movieOrder.getLookDate()) + " " + movieOrder.getEnd() + ":00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(string);
        if (!new Date().after(date)) {
            return Result.fail("请观影之后再评论");
        }
        //设置评分
        MovieItem movieItem = movieItemService.getById(movieComment.getItemId());
        QueryWrapper<MovieComment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MovieComment::getItemId,movieItem.getId());
        List<MovieComment> list = movieCommentService.list(wrapper);
        Float star = 0f;
        for (MovieComment comment : list) {
            star += comment.getStar();
        }
        star+=movieComment.getStar()/(list.size() + 1);
        movieItem.setScore(star);
        movieItemService.updateById(movieItem);
        boolean save = movieCommentService.save(movieComment);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑电影评论 */
    @PostMapping("editMovieComment")
    public Result editMovieComment(@RequestBody MovieComment movieComment) {
        boolean save = movieCommentService.updateById(movieComment);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除电影评论 */
    @GetMapping("removeMovieComment")
    public Result removeMovieComment(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                movieCommentService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("电影评论id不能为空！");
        }
    }

}
